//
//  ViewController.m
//  24-HomeWork MD5 SHA1 DES
//
//  Created by dllo on 16/8/16.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "NSString+DM_DES.h"
#import "NSString+DM_MD5.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *encryTextField;
@property (weak, nonatomic) IBOutlet UITextField *descripTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}
// 加密
- (IBAction)encryButton:(id)sender {
    // 进行post请求
    NSString *postURL = @"http://tool.zzblo.com/api/md5/encrypt";
    [self postOfPostURL:postURL Prefix:@"text=" contents:_encryTextField.text];
}
// 解密
- (IBAction)descripButton:(id)sender {
    // 进行post请求
     NSString *postURL = @"http://tool.zzblo.com/api/md5/decrypt";
    [self postOfPostURL:postURL Prefix:@"secret=" contents:_descripTextField.text];
}
- (IBAction)MD5Button:(id)sender {
    // 转换MD5
   _descripTextField.text = [_descripTextField.text stringWith32Bit_MD5Upper];
    NSLog(@"%@", _descripTextField.text);
}
// post请求方法
- (void)postOfPostURL:(NSString *)url Prefix:(NSString *)prefix contents:(NSString *)contents {
    NSString *postURL = url;
    NSString *parameters = [prefix stringByAppendingString:contents];
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    manger.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    [manger.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];

    [manger POST:postURL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        if ([url isEqualToString:@"http://tool.zzblo.com/api/md5/encrypt"]) {
            NSLog(@"请求成功");
            _descripTextField.text = [responseObject objectForKey:@"secret32"];
            _encryTextField.text = nil;
            NSLog(@"加密后****%@", responseObject);
        } else {
            NSLog(@"请求成功");
            _encryTextField.text = [responseObject objectForKey:@"text"];
            _descripTextField.text = nil;
            NSLog(@"解密后****%@", responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
